﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using EnglishStudy.Entity;
using System.Runtime.Serialization.Json;
using System.ComponentModel;
using GalaSoft.MvvmLight;
using Microsoft.Phone.Shell;

namespace EnglishStudy.ViewModel
{
    public class DictionaryViewModel : ViewModelBase
    {
        DictionaryEntity dictionary = new DictionaryEntity();
        private List<Tuc> meaningList = new List<Tuc>();
        private WordTable a = PhoneApplicationService.Current.State["Word"] as WordTable;
        private string word;

        public string Word
        {
            get { return word; }
            set
            {
                if (word == value)
                {
                    return;
                }
                RaisePropertyChanging("Word");
                word = value;
                RaisePropertyChanged("Word");
            }
        }

        private string wordMeaning;

        public string WordMeaning
        {
            get { return wordMeaning; }
            set
            {
                if (wordMeaning == value)
                {
                    return;
                }
                RaisePropertyChanging("WordMeaning");
                wordMeaning = value;
                RaisePropertyChanged("WordMeaning");
            }
        }

        public List<Tuc> MeaningList
        {
            get { return meaningList; }
            set
            {
                if (meaningList == value)
                {
                    return;
                }
                RaisePropertyChanging("MeaningList");
                meaningList = value;
                RaisePropertyChanged("MeaningList");
            }
        }
        public DictionaryViewModel()
        {
            Word = a.Word.ToUpper();
            WordMeaning = "Meaning of \"" + Word + "\"";
            PhoneApplicationService.Current.State.Remove("Word");
            GetServiceDictionary(eLanguage.English, App.ViewModel.CountryInfo.language, a.Word, true);
        }
        /// <summary>
        /// Get Service of meaning
        /// </summary>
        /// <param name="from"></param>
        /// <param name="dest"></param>
        /// <param name="phrase"></param>
        /// <param name="pretty"></param>
        public void GetServiceDictionary(string from, string dest, string phrase, bool pretty)
        {
            var webClient = new WebClient();
            string HomeTranslate = "http://glosbe.com/gapi/translate?";
            string From = "from=" + from + "&";
            string Dest = "dest=" + dest + "&";
            string Format = "format=json&";
            string Phrase = "phrase=" + phrase + "&";
            string Pretty = "pretty=" + pretty.ToString().ToLower();
            string uri = HomeTranslate + From + Dest + Format + Phrase + Pretty;
            webClient.OpenReadCompleted += OnOpenReadCompleted;
            webClient.OpenReadAsync(new Uri(uri, UriKind.Absolute));
        }

        //Handle Data Response.
        public void OnOpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            var serializer = new DataContractJsonSerializer(typeof(DictionaryEntity));
            var dictionary = (DictionaryEntity)serializer.ReadObject(e.Result);
            List<Tuc> temp = new List<Tuc>();
            temp = new List<Tuc>(dictionary.tuc);
            MeaningList = RemoveEmptyTuc(temp);
        }

        //Remove Tuc have no meaning word.
        public List<Tuc> RemoveEmptyTuc(List<Tuc> temp)
        {
            List<Tuc> result = new List<Tuc>();
            for (int i = 0; i < temp.Count; i++)
            {
                if (string.IsNullOrEmpty(temp[i].WordMeaning))
                {
                    temp.Remove(temp[i]);
                    i--;
                }
            }
            return result = temp;
        }
    }
    public class eLanguage
    {
        public static string Vietnamese = "vie";
        public static string English = "eng";
        public static string France = "fra";
    }
}
